﻿Public Partial Class Wfrm_Mantenedor_Concepto_Relac
    Inherits Wfrm_Master


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ViewState("formulario") = 9 ' Request("ID")
            ViewState("tabla_padre") = "CIUDAD" ' Request("TABLA_PADRE")
            ViewState("usuario") = "jquintanilla"
            Carga_Sp_Busqueda()
            lbl_titulo0.Text = ViewState("tabla")
            lbl_titulo1.Text = ViewState("tabla_padre")
            Label30.Text = Label30.Text & " " & ViewState("tabla_padre")
        End If
    End Sub

    Protected Sub Btn_Buscar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Buscar.Click
        Dim EXEC As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim Rp As New DotNetResponse.SQLPersistence
        Rp.StoredProcedure = "SP_GET_BUSQUEDA_CONCEPTO"
        Rp.AddParametro(Me.txt_busqueda.Text.Trim)
        Rp.AddParametro(ViewState("tabla_padre"))
        EXEC.ExecuteDataTable(Rp)
        Grid_Resultado.DataSource = Rp.DtTable
        Grid_Resultado.DataBind()
        Grid_Resultado.UpdateAfterCallBack = True

        If Not Rp.TieneDatos Then
            MsgInformativo("No se ha encontrado información")
        End If
        If Rp.Errores Then
            MsgError(Rp.MensajeError)
        End If
    End Sub
    Sub Carga_Sp_Busqueda()
        Dim EXEC As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim Rp As New DotNetResponse.SQLPersistence
        EXEC.ExecuteDataTable(Rp, "select * from SYS_FORM where id_form =" & ViewState("formulario"))

        If Rp.TieneDatos Then
            lbl_titulo.Text = Rp.DtTable.Rows(0).Item("titulo_form")
            ViewState("sp_busqueda") = Rp.DtTable.Rows(0).Item("sp_busqueda")
            ViewState("tabla") = Rp.DtTable.Rows(0).Item("tabla")
            ViewState("sp_save") = Rp.DtTable.Rows(0).Item("sp_save")
            ViewState("sp_carga_objetos") = Rp.DtTable.Rows(0).Item("sp_carga_objetos")
            ViewState("sp_delete") = Rp.DtTable.Rows(0).Item("sp_delete")
        End If

    End Sub

    <Anthem.Method()> _
    Public Function Grabar()
        Dim EXECin As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rpi As New DotNetResponse.SQLPersistence
        rpi.AddParametro(Val(txt_ID.Text))
        rpi.AddParametro(txt_codigo.Text.Trim)
        rpi.AddParametro(txt_Descripcion.Text.Trim)
        rpi.AddParametro(Val(txt_ID0.Text.Trim))
        rpi.AddParametro(ViewState("tabla"))
        rpi.StoredProcedure = "SP_SAVE_CONCEPTO"
        EXECin.ExecuteNonQuery(rpi)
        EXECin.Commit()

        If rpi.Errores Then
            MsgError(rpi.MensajeError)
        Else
            MsgInformativo("Grabado con éxito")
            Grid_Resultado_SelectedIndexChanged(Nothing, Nothing)
            Btn_Limpiar_Click(Nothing, Nothing)
        End If

    End Function
    <Anthem.Method()> _
 Public Function Eliminar()
        Dim EXECin As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rpi As New DotNetResponse.SQLPersistence
        rpi.AddParametro(Val(txt_ID.Text))
        rpi.StoredProcedure = "SP_ELIMINAR_CONCEPTO"
        EXECin.ExecuteNonQuery(rpi)
        EXECin.Commit()

        If rpi.Errores Then
            MsgError(rpi.MensajeError)
        Else
            MsgInformativo("Eliminado con éxito")
            Grid_Resultado_SelectedIndexChanged(Nothing, Nothing)
            Btn_Limpiar_Click(Nothing, Nothing)
        End If

    End Function

    Protected Sub Btn_Grabar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Grabar.Click
        If txt_ID0.Text.Trim = "" Then
            MsgInformativo("Seleccione " & ViewState("tabla_padre"))
            Exit Sub
        End If
        'txt_Descripcion

        If txt_ID.Text.Trim = "" Then
            MsgConfirm("¿Confirma Crear un Nuevo Registro?", "function(r){ if(r){Anthem_InvokePageMethod('Grabar',[],function(result) {});} }")
        Else
            MsgConfirm("¿Confirma Modificar el Registro?", "function(r){ if(r){Anthem_InvokePageMethod('Grabar',[],function(result) {});} }")
        End If
    End Sub

    Protected Sub Btn_Eliminar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Eliminar.Click
        MsgConfirm("¿Confirma Eliminar el Registro?", "function(r){ if(r){Anthem_InvokePageMethod('Eliminar',[],function(result) {});} }")
    End Sub

    Protected Sub Grid_Resultado_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grid_Resultado.SelectedIndexChanged
        'SP_GET_BUSQUEDA_CONCEPTO_RELAC

        Dim EXEC As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim Rp As New DotNetResponse.SQLPersistence
        Rp.StoredProcedure = "SP_GET_BUSQUEDA_CONCEPTO_RELAC"
        Rp.AddParametro(Grid_Resultado.SelectedItem.Cells(1).Text.Trim)

        EXEC.ExecuteDataTable(Rp)
        Grid_Resultado0.DataSource = Rp.DtTable
        Grid_Resultado0.DataBind()
        Grid_Resultado0.UpdateAfterCallBack = True

        If Not Rp.TieneDatos Then
            MsgInformativo("No se ha encontrado información")
        End If
        If Rp.Errores Then
            MsgError(Rp.MensajeError)
        End If

        
        txt_ID0.Text = Grid_Resultado.SelectedItem.Cells(1).Text.Trim
        Label35.Text = Grid_Resultado.SelectedItem.Cells(2).Text.Trim
        Label35.UpdateAfterCallBack = True

        txt_ID.Text = ""
        txt_Descripcion.Text = ""
        txt_codigo.Text = ""

        txt_ID0.UpdateAfterCallBack = True
        txt_ID.UpdateAfterCallBack = True
        txt_Descripcion.UpdateAfterCallBack = True
        txt_codigo.UpdateAfterCallBack = True

    End Sub

    Protected Sub Btn_Limpiar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Limpiar.Click

        txt_ID.Text = ""
        txt_Descripcion.Text = ""
        txt_codigo.Text = ""
        Label36.Text = ""

        Label36.UpdateAfterCallBack = True
        txt_ID.UpdateAfterCallBack = True
        txt_Descripcion.UpdateAfterCallBack = True
        txt_codigo.UpdateAfterCallBack = True


    End Sub

    Protected Sub Grid_Resultado0_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grid_Resultado0.SelectedIndexChanged
        txt_ID.Text = Grid_Resultado0.SelectedItem.Cells(1).Text.Trim
        txt_Descripcion.Text = Grid_Resultado0.SelectedItem.Cells(2).Text.Trim
        txt_codigo.Text = Grid_Resultado0.SelectedItem.Cells(3).Text.Trim
        Label36.Text = Grid_Resultado0.SelectedItem.Cells(2).Text.Trim
        Label36.UpdateAfterCallBack = True

        txt_ID.UpdateAfterCallBack = True
        txt_Descripcion.UpdateAfterCallBack = True
        txt_codigo.UpdateAfterCallBack = True

    End Sub
End Class